//
//  HomeTableView.m
//  meituan
//
//  Created by huiwenjiaoyu on 15/12/15.
//  Copyright © 2015年 Rick. All rights reserved.
//

#import "HomeTableView.h"
#import "HomeHeadView.h"
#import "DealTableViewCell.h"
#import "UIView+NavigationController.h"
#import "DealDetailViewController.h"

#define kCellIdentifier @"DealCell"

@interface HomeHeadView()


@end

@implementation HomeTableView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.delegate = self;
        self.dataSource = self;
        self.rowHeight = 80;
        [self registerNib:[UINib nibWithNibName:@"DealTableViewCell" bundle:nil] forCellReuseIdentifier:kCellIdentifier];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
    self = [super initWithFrame:frame style:style];
    if (self) {
        
        self.delegate = self;
        self.dataSource = self;
        self.rowHeight = 80;
        [self registerNib:[UINib nibWithNibName:@"DealTableViewCell" bundle:nil] forCellReuseIdentifier:kCellIdentifier];
    }
    return self;
}

- (void)setHeadInfos:(NSArray *)headInfos
{
    if (_headInfos != headInfos) {
        _headInfos = headInfos;
        
        HomeHeadView *homeHeadView = [[HomeHeadView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenWidth * 0.4)];
        homeHeadView.infos = _headInfos;
        homeHeadView.backgroundColor = [UIColor lightGrayColor];
        self.tableHeaderView = homeHeadView;
    }
}

#pragma  mark - tableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.deals.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DealTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier forIndexPath:indexPath];
    cell.deal = self.deals[indexPath.row];
    return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 35;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 35)];
    label.text = @"猜你喜欢";
    label.textColor = [UIColor blackColor];
    label.font = [UIFont systemFontOfSize:17];
    label.backgroundColor = [UIColor redColor];
    
    return label;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    DealDetailViewController *vc = [[DealDetailViewController alloc] init];
    vc.title = [self.deals[indexPath.row] title];
    vc.deal = self.deals[indexPath.row];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
    
}



@end
